package com.snowy.hidden.user.controller;


import com.snowy.framework.common.base.vo.LoginUser;
import com.snowy.framework.common.resp.BaseResp;
import com.snowy.framework.common.resp.BaseRespUtil;
import com.snowy.framework.exception.UABPException;
import com.snowy.hidden.constant.AuthConstant;
import com.snowy.hidden.constant.AuthErrorCode;
import com.snowy.hidden.constant.AuthErrorENU;
import com.snowy.hidden.exception.SysErrorEnum;
import com.snowy.hidden.interceptor.AuthorHandle;
import com.snowy.hidden.interceptor.IgnoreAuth;
import com.snowy.hidden.interceptor.IgnoreLogin;
import com.snowy.hidden.user.domain.dto.LoginPwdDTO;
import com.snowy.hidden.user.domain.dto.UserInfoDTO;
import com.snowy.hidden.user.domain.model.SysUser;
import com.snowy.hidden.user.domain.req.GetWechatPhoneReq;
import com.snowy.hidden.user.domain.req.WechatLoginReq;
import com.snowy.hidden.user.mapper.SysUserMapper;
import com.snowy.hidden.user.service.AuthService;
import com.snowy.hidden.util.SeUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;

@Api(tags = "权限认证")
@Slf4j
@RestController
@RequestMapping("/api/user")
public class AuthController {


    @Autowired
    private AuthService authService;

    @Autowired
    private SysUserMapper sysUserMapper;




    @IgnoreAuth
    @ApiOperation("(组织)用户密码登录")
    @PostMapping(value = "/loginPWD")
    public Object loginPWDRole(@Valid @RequestBody(required = true) LoginPwdDTO loginPwdDTO, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return BaseRespUtil.Error(AuthErrorENU.PARAM_ERROR.getCode(), bindingResult.getAllErrors().get(0).getDefaultMessage());
        }
        try {
            String phoneNumber = loginPwdDTO.getPhone();
            String password = loginPwdDTO.getPassword();
            UserInfoDTO userInfoDTO = this.authService.loginPWDRole(phoneNumber, password);
            return BaseRespUtil.Success(userInfoDTO);
        } catch (Exception e) {
            return BaseRespUtil.Error(e);
        }
    }



//    @ApiOperation("注销登录")
//    @PostMapping(value = "/logout")
//    public Object logout() {
//
//        try {
//            LoginUser userInfo = AuthConstant.userLocal.get();
//            if (userInfo == null) {
//                return BaseRespUtil.Success();
//            }
//            this.authService.logout(userInfo);
//            return BaseRespUtil.Success();
//        } catch (Exception e) {
//            return BaseRespUtil.Error(e);
//        }
//    }

//    @IgnoreAuth
//    @ApiOperation("token登录")
//    @GetMapping(value = "/validateToken")
//    public BaseResp validateToken(@ApiParam(value="token" ,required=true)@RequestParam("token") String token) {
//        try {
//            token= SeUtil.decryptStr(token);
//            SysUser sysUser = AuthorHandle.getUserInfoByToken(token);
//            if (sysUser == null) {
//                return BaseRespUtil.Error(AuthErrorENU.NO_ASSTOKEN.getCode(),AuthErrorENU.NO_ASSTOKEN.getMsg());
//            }else {
//                return  BaseRespUtil.Success(sysUser);
//            }
//        } catch (Exception e) {
//            return BaseRespUtil.Error(e);
//        }
//    }



    @ApiOperation("用户密码登录")
    @PostMapping(value = "/listsysuser")
    public BaseResp<List<SysUser>> listsysuser() {
        try {

            List<SysUser> userList= this.sysUserMapper.selectList(null);
            return BaseRespUtil.Success(userList);
        } catch (Exception e) {
            return BaseRespUtil.Error(e);
        }
    }


//    @IgnoreAuth
//    @ApiOperation("微信小程序登录")
//    @PostMapping(value = "/appletLogin")
//    public BaseResp<LoginUser> appletLogin(@Valid @RequestBody WechatLoginReq req) {
//        LoginUser loginUser = this.authService.appletLogin(req);
//        return BaseRespUtil.Success(loginUser);
//    }


    @IgnoreLogin
    @IgnoreAuth
    @ApiOperation("微信登录")
    @PostMapping(value = "/loginByWechat")
    public BaseResp loginByWechat(@Valid @RequestBody WechatLoginReq wechatLoginReq, BindingResult bindingResult, HttpServletRequest request) {
        if (bindingResult.hasErrors()) {
            return BaseRespUtil.Error(AuthErrorCode.PARAM_ERROR.getReturnCode(),bindingResult.getAllErrors().get(0).getDefaultMessage());
        }
        try {
            BaseResp baseResp = this.authService.loginByWechat(wechatLoginReq,request);
            return baseResp;
        } catch (UABPException e){
            return BaseRespUtil.Error(e);
        } catch (Exception e) {
            log.error("==loginByWechat=="+e.getMessage());
            return BaseRespUtil.Error(AuthErrorCode.INFT_ERROR);
        }
    }

    @ApiOperation("获取用户手机号")
    @PostMapping(value = "/getWechatPhone")
    public BaseResp getWechatPhone(@Valid @RequestBody GetWechatPhoneReq getWechatPhoneReq, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return BaseRespUtil.Error(SysErrorEnum.PARAM_ERROR.getReturnCode(), bindingResult.getAllErrors().get(0).getDefaultMessage());
        }
        try {
            //UserInfoDto userRedisInfo = AuthConstant.userLocal.get();
            String phone = this.authService.getWechatPhone(getWechatPhoneReq,getWechatPhoneReq.getAccessToken());
            return BaseRespUtil.Success(phone);
        } catch (Exception e) {
            return BaseRespUtil.Error(e);
        }
    }

}

